package com.xhwl.centiotdata.handler.vehicle.impl;

import com.xhwl.centiotdata.handler.vehicle.IVehicleIntegrationHandler;
import com.xhwl.centiotdata.service.cent.IVehicleCentService;
import com.xhwl.centiotdata.suppliersystem.vehicle.ketop.KeTopHandler;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForInFeignDTO;
import com.xhwl.common.dto.cent.vehicle.feign.CreateVehicleAccessRecordsForOutFeignDTO;
import com.xhwl.common.dto.cent.vehicle.http.ketop.*;
import com.xhwl.common.dto.cent.vehicle.http.ketop.query.QueryCommonKeTopDTO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleParkingInfoBO;
import com.xhwl.common.pojo.cent.vehicle.bo.feign.CreateVehicleAccessRecordsForInFeignBO;
import com.xhwl.common.pojo.cent.vehicle.bo.feign.CreateVehicleAccessRecordsForOutFeignBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.*;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryAccessRecordBO;
import com.xhwl.common.pojo.cent.vehicle.bo.http.query.QueryFreeSpaceBO;
import com.xhwl.common.pojo.cent.vehicle.converter.feign.AccessRecordFeignServiceConverter;
import com.xhwl.common.pojo.cent.vehicle.converter.http.HttpQueryParameterConverter;
import com.xhwl.common.pojo.cent.vehicle.converter.http.HttpResponseConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class KeTopIntegrationHandler implements IVehicleIntegrationHandler {

    @Autowired
    private KeTopHandler keTopHandler;

    @Autowired
    private HttpQueryParameterConverter httpQueryParameterConverter;

    @Autowired
    private HttpResponseConverter httpResponseConverter;

    @Autowired
    private IVehicleCentService vehicleCentService;

    @Autowired
    private AccessRecordFeignServiceConverter accessRecordFeignServiceConverter;

    @Override
    public FreeSpaceBO queryFreeSpace(VehicleParkingInfoBO vehicleParkingInfoBO, QueryFreeSpaceBO queryFreeSpaceBO) {

        FreeSpaceNumberKeTopDTO freeSpaceNumberKeTopDTO = keTopHandler.queryFreeSpace(vehicleParkingInfoBO, httpQueryParameterConverter.toQueryFreeSpaceNumberKeTopDTO(queryFreeSpaceBO));

        return httpResponseConverter.toFreeSpaceBO(freeSpaceNumberKeTopDTO);
    }

    @Override
    public ParkingLotInfoBO queryParkingLotInfo(VehicleParkingInfoBO vehicleParkingInfoBO) {

        ParkingLotInfoKeTopDTO parkingLotInfoKeTopDTO = keTopHandler.queryParkingLotInfo(vehicleParkingInfoBO, new QueryCommonKeTopDTO());

        return httpResponseConverter.toParkingLotInfoBO(parkingLotInfoKeTopDTO);
    }

    @Override
    public ParkingLotAreaBO queryParkingLotArea(VehicleParkingInfoBO vehicleParkingInfoBO) {

        ParkingLotAreaKeTopDTO parkingLotAreaKeTopDTO = keTopHandler.queryParkingLotArea(vehicleParkingInfoBO, new QueryCommonKeTopDTO());

        return httpResponseConverter.toParkingLotAreaBO(parkingLotAreaKeTopDTO);
    }

    @Override
    public ParkingLotPassageBO queryParkingLotPassage(VehicleParkingInfoBO vehicleParkingInfoBO) {

        ParkingLotPassageKeTopDTO parkingLotPassageKeTopDTO = keTopHandler.queryParkingLotPassage(vehicleParkingInfoBO, new QueryCommonKeTopDTO());

        return httpResponseConverter.toParkingLotPassageBO(parkingLotPassageKeTopDTO);
    }


    /**
     * 入场记录
     * @param vehicleIntegrationConfigBO
     * @param queryAccessRecordBO
     * @return
     */
    @Override
    public ParkingLotAccessRecordForInBO queryAccessRecordForIn(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<AccessRecordKeTopDTO> accessRecordKeTopDTO = keTopHandler.getCarInOutInfo(vehicleIntegrationConfigBO.getVehicleParkingInfoBO(), queryAccessRecordBO, 0);
        return httpResponseConverter.toParkingLotAccessRecordForInBO(vehicleIntegrationConfigBO, accessRecordKeTopDTO);
    }

    /**
     * 出场记录
     * @param vehicleIntegrationConfigBO
     * @param queryAccessRecordBO
     * @return
     */
    @Override
    public ParkingLotAccessRecordForOutBO queryAccessRecordForOut(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<AccessRecordKeTopDTO> accessRecordKeTopDTO = keTopHandler.getCarInOutInfo(vehicleIntegrationConfigBO.getVehicleParkingInfoBO(), queryAccessRecordBO, 1);
        return httpResponseConverter.toParkingLotAccessRecordForOutBO(vehicleIntegrationConfigBO, accessRecordKeTopDTO);
    }

    /**
     * 查询缴费记录
     * @param vehicleIntegrationConfigBO
     * @param queryAccessRecordBO
     * @return
     */
    @Override
    public ParkingLotPayRecordBO queryPayRecord(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordBO queryAccessRecordBO) {
        List<PayRecordKeTopDTO> payRecordKeTopDTO = keTopHandler.getParkingPaymentList(vehicleIntegrationConfigBO.getVehicleParkingInfoBO(), queryAccessRecordBO);
        return httpResponseConverter.toParkingLotPayRecordBO(vehicleIntegrationConfigBO, payRecordKeTopDTO);
    }


    /**
     * 将进记录加到车行中台
     * @param createVehicleAccessRecordsForInFeignBO
     */
    @Override
    public void addAccessRecordsForIn(CreateVehicleAccessRecordsForInFeignBO createVehicleAccessRecordsForInFeignBO) {
        CreateVehicleAccessRecordsForInFeignDTO createVehicleAccessRecordsForInFeignDTO = accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForInFeignDTO(createVehicleAccessRecordsForInFeignBO);
        try {
            vehicleCentService.addAccessRecordsForIn(createVehicleAccessRecordsForInFeignDTO);
        } catch (Exception e) {
            log.error("科拓入场数据无法给到车行中台");
        }
    }

    /**
     * 将出记录添加到车行中台
     * @param createVehicleAccessRecordsForOutFeignBO
     */
    @Override
    public void addAccessRecordsForOut(CreateVehicleAccessRecordsForOutFeignBO createVehicleAccessRecordsForOutFeignBO) {
        CreateVehicleAccessRecordsForOutFeignDTO createVehicleAccessRecordsForOutFeignDTO = accessRecordFeignServiceConverter.toCreateVehicleAccessRecordsForOutFeignDTO(createVehicleAccessRecordsForOutFeignBO);
        try {
            vehicleCentService.addAccessRecordsForOut(createVehicleAccessRecordsForOutFeignDTO);
        } catch (Exception e) {
            log.error("科拓出场记录数据无法给到车行中台");
        }
    }

    /**
     * 将缴费记录添加到车行中台
     * @param parkingLotPayRecordBO
     */
    @Override
    public void addPayRecords(ParkingLotPayRecordBO parkingLotPayRecordBO) {
        try {
            vehicleCentService.addPayRecords(parkingLotPayRecordBO);
        } catch (Exception e) {
            log.error("科拓缴费记录数据无法给到车行中台");
        }
    }
}
